#ifndef _CSF_LMTP_H
#define _CSF_LMTP_H

#include "queue.h"
#include <mfile.h>

#include <sys/param.h>
#include <limits.h>
#include <parsemail/parsemail.h>


#ifdef __cplusplus
extern "C" {
#endif

#ifndef IOV_MAX
#define IOV_MAX 1024
#endif

/* writev() limited */
#define MAX_BLOCK_NUMBER    IOV_MAX

/* 
* LMTP data buffer block size,
* for better performace, set it to 256KB 
*/
#define BUFFER_BLOCK_SIZE    262144

/* RCPT address length */
#define MAIL_ADDRESS_LENGTH    255

/* The maximal length of a IP address */
#define MAX_IP_LEN            15

/* RCPT structure for each RCPT address */
typedef struct rcpt{
    STAILQ_ENTRY(rcpt)    rcpt_entries;       /* rcpt queue field */
    int                    rc;        /* keep each rcpt return code */
    unsigned int        rcpt_len;
    char                rcpt[MAIL_ADDRESS_LENGTH + 1]; /* preserve the rcpt to address */
    char                path[PATH_MAX];     /* mail body file path */
} RCPT;

/* Save the mail information for every lmtp conversation */
typedef struct mail_info {
    STAILQ_HEAD(, rcpt)        rcpt_head; /* rcpt queue head */
    int                        rcpt_n;    /* rcpt number */
    time_t                    time;      /* the mail received time */
    uint32_t                ip_addr;
    char                    ip_str[MAX_IP_LEN + 1];   /* origin IP */
} MAIL_INFO;

/* LMTP protocol data structure */
typedef struct lmtp_data {
    MAIL_INFO		mi; /* lmtp session info */
    MAIL_HANDLE*	pm; /* parse mail handler */
    MFILE			*data_buffer; /* mail data */
    void			*data;       /* lmtp state */
    char			command_file[PATH_MAX + 1]; /* store ini file location */
    char			file_path[PATH_MAX + 1]; /* mail location */
} LMTP_DATA;

#ifdef __cplusplus
}
#endif

#endif

